package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;



    /**
    * @Description: 添加购物车
            * @Param: 
            * @return: 
            * @Author: Lei
            * @Date: 
     */
    
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据:{}",shoppingCart);

        /**设置用户id，指定是当前哪个用户的购物车数据*/
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);


        /**查询当前菜品或者套餐是否在购物车中*/
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);

        if(dishId!=null){
            /**添加到购物车的是菜品*/
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
//            lambdaQueryWrapper.eq(ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());


        }else{
            /**添加到购物车的是套餐*/
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart cartServiceOne = shoppingCartService.getOne(lambdaQueryWrapper);

        if(cartServiceOne!=null){
            /**如果已经存在，在原来的数量基础上加一*/
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(cartServiceOne);
        }else{
            /**如果不存在，则添加到购物车，数量默认为1*/
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne=shoppingCart;
        }


        return R.success(cartServiceOne);
    }


    /**
    * @Description: 查看购物车
            * @Param:
            * @return:
            * @Author: Lei
            * @Date:
     */
    
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车...");

        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        lambdaQueryWrapper.orderByAsc(ShoppingCart::getCreateTime);


        List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);


        return R.success(list);
    }

    /**
    * @Description: 清空购物车
            * @Param:
            * @return:
            * @Author: Lei
            * @Date:
     */

    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("购物车已清空");
    }

    /**
    * @Description: 减少购物车物品数量
            * @Param: 
            * @return: 
            * @Author: Lei
            * @Date: 
     */
    
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据删除版:{}",shoppingCart);
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null){
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
            ShoppingCart cartServiceOne = shoppingCartService.getOne(lambdaQueryWrapper);
            Integer number = cartServiceOne.getNumber();
            if (number>1) {
                cartServiceOne.setNumber(number-1);
                shoppingCartService.updateById(cartServiceOne);
            }else{
                shoppingCartService.remove(lambdaQueryWrapper);
            }
        }else{
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            ShoppingCart cartServiceOne = shoppingCartService.getOne(lambdaQueryWrapper);
            Integer number = cartServiceOne.getNumber();
            if (number>1) {
                cartServiceOne.setNumber(number-1);
            }else{
                shoppingCartService.remove(lambdaQueryWrapper);
            }
        }
        return R.success("");
    }
}
